// pages/buy/buy.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cart: [], //购物车数据
    wordLength: 0,//备注长度
    array: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
    tablewarenum: 0,//餐具数量
    beizhu:'',
    moeny:0,
    num:0

  },
  



  input(e) {
    let input = e.detail.value
    let wordLength = parseInt(input.length);
    if (wordLength > 50) {
      return;
    }
    if (wordLength == 50) {
      wx.showToast({
        icon: 'none',
        title: '字数已到最大限制',
      })
    }
    this.setData({
      wordLength: wordLength,
      beizhu:input
    });
  },
  bindGenderChange: function (e) {
    let tablewarenum = parseInt(e.detail.value) + 1
    console.log("餐具数量为", tablewarenum);
    this.setData({
      index: e.detail.value,
      tablewarenum: tablewarenum
    })
  },
  //提交订单
  buy() {
    let user = wx.getStorageSync('user')
     if (this.data.tablewarenum) {
      console.log("点击了提交订单");
      wx.cloud.database().collection('order')
        .add({
          data: {
            name: user.nickName,//
            beizhu: this.data.beizhu,//
            tablewarenum:this.data.tablewarenum,//餐具数量
            zhuohao: app.globalData.zhuohao,//
            totalPrice: this.data.moeny, //总价钱
            orderList: this.data.cart, //存json字符串,用户购买菜品
            status: 1, //1新下单待上餐,2待用户评价,3订单已完成
            _createTime: this.getCurrentTime() //创建的时间
          }
        }).then(res=>{
          //增加销量
          let foods  = []
      this.data.cart.forEach(item=>{
        let food = {}
        food._id = item._id
        food.num=item.num
        foods.push(food)
      })
      wx.cloud.callFunction({
        name:'addSell',
        data:{
          foods:foods
        },
      })
          wx.removeStorageSync('cart')
          wx.navigateTo({
            url: '/pages/success/success',
          })
        })
    } else {
      wx.showToast({
        icon: 'none',
        title: '您还没有选择餐具数量',
      })
    }
  },
  // 获取当前时间
  getCurrentTime() {
    let d = new Date();
    let month = d.getMonth() + 1;
    let date = d.getDate();
    let hours = d.getHours();
    let minutes = d.getMinutes();

    let curDateTime = d.getFullYear() + '年';
    if (month > 9)
      curDateTime += month + '月';
    else
      curDateTime += month + '月';

    if (date > 9)
      curDateTime = curDateTime + date + "日";
    else
      curDateTime = curDateTime + date + "日";
    if (hours > 9)
      curDateTime = curDateTime + hours + "时";
    else
      curDateTime = curDateTime + hours + "时";
    if (minutes > 9)
      curDateTime = curDateTime + minutes + "分";
    else
      curDateTime = curDateTime + minutes + "分";
    return curDateTime;
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let cart = wx.getStorageSync('cart') //从缓存中获取数据
    let moeny = 0
    let num = 0
    console.log(cart);
    cart.forEach(item => {
      moeny += item.num * item.price,
        num += item.num
    })
    this.setData({
      cart: cart,
      moeny: moeny,
      num: num,
      zhuohao:app.globalData.zhuohao
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})